import registnet.dao.UnitUserPeer;
import registnet.dao.UnitUser;
import registnet.dao.SystemLogPeer;

/* [start]	defines a renderer for the grades */
private class UserRenderer implements ListitemRenderer  {
	public void render(Listitem item, java.lang.Object data) {
		new Listcell(data[0]).setParent(item);
		new Listcell(data[1]).setParent(item);
	}
}
/* [end] */

retrieveUsers(){
	List users = new UnitUserPeer().getActiveUsers();
	
	String[][] listModel = new String[users.size()][2];
	
	for(int i=0; i<users.size(); i++){
		UnitUser user = users.get(i);
		listModel[i][0] = user.getUsername();
		listModel[i][1] = user.getUnit().getUnitCode();
	}
	
	ListModel strset = new SimpleListModel(listModel);
	users_lb.setModel(strset);
	users_lb.setItemRenderer(new UserRenderer());
}

void deactivate(){
	UnitUserPeer uup = new UnitUserPeer();
	if(users_lb.getSelectedCount() != 0){
		int isConfirmed = Messagebox.show("Proceed?", "Deactivate User", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
		if (isConfirmed == Messagebox.YES){
			Iterator i = users_lb.getSelectedItems().iterator();
			while(i.hasNext()){
				Listitem listitem = i.next(); 
				UnitUser uu = uup.getUnitUser(listitem.getLabel());
				uup.deactivate(uu.getUserId());
				SystemLogPeer.createNewSystemLog(uu.getUsername() +" Unit user was deactivated.", "registnet", "Administrator");
			}
			retrieveUsers();
		}			
	}
	else{
		alert("Select one user");
	}
}

void delete(){
	UnitUserPeer uup = new UnitUserPeer();
	if(users_lb.getSelectedCount() != 0){
		int isConfirmed = Messagebox.show("Proceed?", "Activate User", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
		if (isConfirmed == Messagebox.YES){
			Iterator i = users_lb.getSelectedItems().iterator();
			while(i.hasNext()){
				Listitem listitem = i.next(); 
				UnitUser uu = uup.getUnitUser(listitem.getLabel());
				uup.deleteUnitUser(uu.getUserId());
				SystemLogPeer.createNewSystemLog(uu.getUsername() +" Unit user was deleted.", "registnet", "Administrator");
			}
			retrieveUsers();
		}			
	}
	else{
		alert("Select one user");
	}
}
